package 分类.二叉树;
class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, Node _left, Node _right, Node _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }
};
public class 填充每个节点的下一个右侧节点指针 {
    public Node connect(Node root) {
       if(root==null) return null;
       build(root.left,root.right);
       return root;
    }

    void build(Node left,Node right){
        if(left==null||right==null) return;
        left.next=right;
        build(left.left,left.right);
        build(right.left,right.right);
        build(left.right,right.left);
    }
}
